using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ADODB;
using Microsoft.CommerceServer.Interop.Configuration;

namespace Microsoft.CommerceServer.SecureSite
{
	public class CommerceSite : Microsoft.CommerceServer.SecureSite.Wizard
	{
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Label lblCommerceSite;
		private System.Windows.Forms.ComboBox cmbBoxCommerceSites;
		private System.Windows.Forms.Label lbSitelInstructions;
		private System.ComponentModel.IContainer components = null;

		public CommerceSite()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.CommerceSite_Load);
		}
		#endregion

		protected override void btnNext_Click(object sender, System.EventArgs e)
		{
			if(CommerceSecurityInfo.SiteName!=null)
			{
				// did our value changed?
				if(CommerceSecurityInfo.SiteName!=cmbBoxCommerceSites.Text)
				{
					CommerceSecurityInfo.SiteName = cmbBoxCommerceSites.Items[cmbBoxCommerceSites.SelectedIndex].ToString();
					CommerceSecurityInfo.sitecfg.Initialize(CommerceSecurityInfo.SiteName);
					CommerceSecurityInfo.ary = (System.Array)CommerceSecurityInfo.sitecfg.GetAppsInSite(ref CommerceSecurityInfo.SiteName);
					CommerceSecurityInfo.IsDirty = true;
				}
				else
				{
					CommerceSecurityInfo.IsDirty = false;
				}
			}
			else
			{
				CommerceSecurityInfo.SiteName = cmbBoxCommerceSites.Items[cmbBoxCommerceSites.SelectedIndex].ToString();
				CommerceSecurityInfo.ary = (System.Array)CommerceSecurityInfo.sitecfg.GetAppsInSite(ref CommerceSecurityInfo.SiteName);
				CommerceSecurityInfo.sitecfg.Initialize(CommerceSecurityInfo.SiteName);
			}
			CommerceSecurityInfo.MoveToForm("CommerceAccount");
			base.Visible = false;
		}

		protected override void btnPrevious_Click(object sender, System.EventArgs e)
		{
			CommerceSecurityInfo.MoveToForm("Welcome");
			base.Visible = false;
		}

		private void CommerceSite_Load(object sender, System.EventArgs e)
		{
			this.panel3 = new System.Windows.Forms.Panel();
			this.cmbBoxCommerceSites = new System.Windows.Forms.ComboBox();
			this.lblCommerceSite = new System.Windows.Forms.Label();
			this.lbSitelInstructions = new System.Windows.Forms.Label();
			this.panel3.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnNext
			// 
			this.btnNext.Name = "btnNext";
			// 
			// btnPrevious
			// 
			this.btnPrevious.Name = "btnPrevious";
			// 
			// panel3
			// 
			this.panel3.Controls.Add(this.cmbBoxCommerceSites);
			this.panel3.Controls.Add(this.lblCommerceSite);
			this.panel3.Controls.Add(this.lbSitelInstructions);
			this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel3.Location = new System.Drawing.Point(0, 72);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(552, 262);
			this.panel3.TabIndex = 4;
			// 
			// cmbBoxCommerceSites
			// 
			this.cmbBoxCommerceSites.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbBoxCommerceSites.Location = new System.Drawing.Point(216, 128);
			this.cmbBoxCommerceSites.Name = "cmbBoxCommerceSites";
			this.cmbBoxCommerceSites.Size = new System.Drawing.Size(232, 21);
			this.cmbBoxCommerceSites.TabIndex = 2;
			// 
			// lblCommerceSite
			// 
			this.lblCommerceSite.Location = new System.Drawing.Point(88, 128);
			this.lblCommerceSite.Name = "lblCommerceSite";
			this.lblCommerceSite.Size = new System.Drawing.Size(120, 23);
			this.lblCommerceSite.TabIndex = 1;
			this.lblCommerceSite.Text = "Select Commerce Site:";
			// 
			// lbSitelInstructions
			// 
			this.lbSitelInstructions.Location = new System.Drawing.Point(40, 16);
			this.lbSitelInstructions.Name = "lbSitelInstructions";
			this.lbSitelInstructions.Size = new System.Drawing.Size(472, 96);
			this.lbSitelInstructions.TabIndex = 0;
			// 
			// CommerceSite
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 374);
			this.Controls.Add(this.panel3);
			this.Name = "CommerceSite";
			this.Controls.SetChildIndex(this.panel3, 0);
			this.panel3.ResumeLayout(false);
			this.ResumeLayout(false);

			CommerceSecurityInfo.Log("FORM: CommerceSite form loaded");
			base.Title = "Select Commerce Site";
			this.lbSitelInstructions.Text = "Select a Commerce Site to secure. Click the Next button to continue.";

			loadCommerceSites();
		}

		private void loadCommerceSites()
		{
			_Recordset rs = CommerceSecurityInfo.glb2.Sites();

			// make sure we have site to work with
			if(rs!=null)
			{
				foreach(Field fld in rs.Fields)
				{
					cmbBoxCommerceSites.Items.Add(fld.Value.ToString());
				}
				
				cmbBoxCommerceSites.SelectedIndex = 0;
			}
		}
	}
}

